package com.redlion.weapon;

import java.util.ArrayList;
import java.util.HashMap;

import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.util.Log;

import com.redlion.Game;
import com.redlion.GameActivity;
import com.redlion.hud.MyHUD;
import com.redlion.hud.WeaponEntrySprite;

public class WeaponAdapter implements CatalogAdapter {

	private IPlanetWeapon w;
	private ArrayList<IdHolder> idHolder = new ArrayList<IdHolder>();
	static ArrayList<IdHolder> staticWeaponIdHolderList = new ArrayList<IdHolder>();
	static HashMap<Integer, ArrayList<IdHolder>> staticUpgradeIdHolderMap = new HashMap<Integer, ArrayList<IdHolder>>();
	private Font mFont;
	private ITextureRegion mCatalogItemBackground_TR;
	static {
		for (WeaponEntry we : WeaponCatalog.weapons) {
			staticWeaponIdHolderList.add(new IdHolder(we.weaponIndex, 0));
			ArrayList<IdHolder> tempIdHolder = new ArrayList<IdHolder>();
			for (Upgrade u : we.getUpgrades()) {
				tempIdHolder.add(new IdHolder(we.weaponIndex, u.index));
			}
			staticUpgradeIdHolderMap.put(we.weaponIndex, tempIdHolder);
		}
	}

	public WeaponAdapter(IPlanetWeapon w, Font pFont, ITextureRegion pCatalogItemBackground_TR) {
		this.w = w;
		this.mFont = pFont;
		this.mCatalogItemBackground_TR=pCatalogItemBackground_TR;
		init();
	}

	private void init() {
		if (w.getWeaponID() < 0) {
			for (WeaponEntry we : WeaponCatalog.weapons) {
				idHolder.add(new IdHolder(we.weaponIndex, 0));
			}
		} else {
			for (Upgrade u : WeaponCatalog.getWeapon(w.getWeaponID()).getUpgrades()) {
				idHolder.add(new IdHolder(w.getWeaponID(), u.index));
			}
		}
	}

	@Override
	public int getCatalogItemSize() {
		return idHolder.size();
	}

	@Override
	public WeaponEntrySprite getCatalogItem(final int position, Sprite view) {
		WeaponEntrySprite s;
		Upgrade u = WeaponCatalog.getUpgrade(idHolder.get(position).weaponIndex, idHolder.get(position).upgradeIndex);
		String name = WeaponCatalog.getWeapon(idHolder.get(position).weaponIndex).name;
		if (view != null) {
			s = (WeaponEntrySprite) view;
		} else {
			s = new WeaponEntrySprite(0, MyHUD.WEAPONSLOT_HEIGHT * position, MyHUD.WEAPONSLOT_WIDTH, MyHUD.WEAPONSLOT_HEIGHT,
					WeaponCatalog.getWeapon(idHolder.get(position).weaponIndex).mTiledTextureRegion, mCatalogItemBackground_TR,
					Game.gameActivity.getVertexBufferObjectManager(), mFont);
		}
		s.setTileIndex(WeaponCatalog.getWeapon(idHolder.get(position).weaponIndex).mTileIndex);
		s.setName(name);
		if (u != null) {
			s.setPrice(u.price);
			s.setDelay(u.delay);
			s.setDamage(u.damage);
		}
		if (u.index == w.getUpgradeID() + 1)
			s.setState(WeaponEntrySprite.NEXT);
		else
			s.setState(WeaponEntrySprite.DEFAULT);
		return s;
	}

	public IPlanetWeapon getWeapon() {
		return w;
	}

	@Override
	public IdHolder getItem(int position) {
		return idHolder.get(position);
	}

	@Override
	public void onItemClick(IdHolder userData) {
		w.updateAnimation(WeaponCatalog.getUpgrade(userData.weaponIndex, userData.upgradeIndex),
				WeaponCatalog.getWeapon(userData.weaponIndex).textureIndices);
		w.setIdHolder(userData);
		w.setFireListener(WeaponCatalog.getOnFireListener(userData.weaponIndex));
		idHolder = staticUpgradeIdHolderMap.get(userData.weaponIndex);
	}
}
